package com.atguigu.servicecms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.servicecms.entity.CrmBanner;
import com.atguigu.servicecms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author JY
 * @since 2021-01-27
 */
@RestController
@RequestMapping("/servicecms/banner")
//@CrossOrigin
public class CrmBannerController {
    @Autowired
    private CrmBannerService bannerService;

    //分页
    @GetMapping("page/{page}/{limit}")
    public R pageList(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> pageParam = new Page<>(page,limit);
        bannerService.page(pageParam,null);
        long total = pageParam.getTotal();
        List<CrmBanner> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @CacheEvict(value = "banner",allEntries = true)
    //新增
    @PostMapping("add")
    public R add(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @CacheEvict(value = "banner",allEntries = true)
    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @CacheEvict(value = "banner",allEntries = true)
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }
}

